package com.example.tourism.service.impl;

import com.example.tourism.mapper.HotelMapper;
import com.example.tourism.mapper.UserMapper;
import com.example.tourism.pojo.Result;
import com.example.tourism.pojo.po.*;
import com.example.tourism.pojo.vo.res.CommentRes;
import com.example.tourism.pojo.vo.res.HotelResVo;
import com.example.tourism.pojo.vo.res.ImgResVo;
import com.example.tourism.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.example.tourism.pojo.Result.success;

@Service
public class HotelServiceImpl implements HotelService {
    @Autowired
    HotelMapper  hotelMapper;
    @Autowired
    UserMapper  userMapper;

    @Override
    public Result addHotel(HotelInfo hotelInfo) {
        // 入驻酒店
        // 1. 判断是否已经入驻
        HotelInfo hotelInfo1 = hotelMapper.selectHotelByUserId(String.valueOf(hotelInfo.getUserId()));
        if(hotelInfo1 != null){
            return Result.error(201, "已申请入驻");
        }
        // 2. 插入数据库
        int res = hotelMapper.insertHotel(hotelInfo);
        if(res > 0){
            return success();
        }
        return Result.error(500,"申请入驻失败！");
    }

    @Override
    public Result getIsApplyHotel(String userId) {
        System.out.println("userId:" + userId);
        HotelInfo hotelInfo = hotelMapper.selectHotelByUserId(userId);
        if (hotelInfo != null) {
            System.out.println("hotelInfo存在,已申请入驻");
            return success(hotelInfo);
        }else {
            return Result.error(201, "未申请入驻");
        }
    }

    @Override
    public Result fetchApplicationList(String id) {
        // 先检测是否是管理员账号
        User user = userMapper.selectById(id);
        if(user != null) {
            if (user.getRole().equals("admin")) {
                //查询所有申请入驻的酒店
                List<HotelInfo> hotelInfos = hotelMapper.selectAllHotel();
                return success(hotelInfos);
            }else{
                return Result.error(201, "非管理员账号");
            }
        }else {
            return Result.error(400, "账号id错误");
        }
    }

    @Override
    public Result approveApplication(String id) {

       int res = hotelMapper.approveApplication(id);
       int ans = userMapper.updateUserRole(id, "shopkeeper");
       if(  res > 0  && ans > 0){
           return success();
       }else {
           return Result.error(500, "审核失败");
       }
    }

    @Override
    public Result rejectApplication(String id) {
        int res = hotelMapper.rejectApplication(id);
        //int ans = userMapper.updateUserRole(id, "traveler");
        if(  res > 0 ){
            return success();
        }else {
            return Result.error(500, "审核失败");
        }
    }

    @Override
    public Result withdrawApplication(String id) {
         int res = hotelMapper.withdrawApplication(id);
         int ans = userMapper.updateUserRole(id, "traveler");
         if(  res > 0  && ans > 0){
             return success();
         }else {
             return Result.error(500, "撤回失败");
         }
    }

    @Override
    public Result insertRoom(Room room,String id) {
         int res = hotelMapper.insertRoom(room,id);
         if(  res > 0 && room.getId() != null){
             return success(room.getId());
         }else {
             return Result.error(500, "添加房间失败");
         }
    }

    @Override
    public Result getHotelRoomList(String id) {
        List<Room> rooms = hotelMapper.getHotelRoomList(id);
        if (rooms != null) {
            return success(rooms);
        } else {
            return Result.error(500, "获取房间列表失败");
        }
    }

    @Override
    public Result getRoomPhotos(String hotelId) {
        List<ImgResVo>  imgResVos = hotelMapper.getRoomPhotos(hotelId);
        if (imgResVos != null) {
            return success(imgResVos);
        } else {
            return Result.error(500, "获取房间图片失败");
        }
    }

    @Override
    public Result updateRoomInfo(Room room) {
        int res = hotelMapper.updateRoomInfo(room);
        if(res > 0){
            return success();
        }else{
            return Result.error(500, "更新失败");
        }
    }

    @Override
    public Result uploadImg(Img img) {
        if(img  != null) {
          hotelMapper.deleteRoomPhoto(img.getToId());
          hotelMapper.InsertRoomPhoto(img);
          return success();
        }else{
            return Result.error(500, "上传失败");
        }

    }

    @Override
    public Result getHotelInfo(String id) {
         HotelInfo hotelInfo = hotelMapper.getHotelInfo(id);
         // 得到酒店的照片
         List<ImgResVo>  imgResVos = hotelMapper.getHotelPhotos(id);
         HotelResVo  hotelResVo = new HotelResVo();

         hotelResVo.setId(hotelInfo.getId());
         hotelResVo.setName(hotelInfo.getHotelName());
         hotelResVo.setAddress(hotelInfo.getHotelAddress());
         hotelResVo.setPhone(hotelInfo.getContactPhone());
         hotelResVo.setEmail(  hotelInfo.getEmail());
         hotelResVo.setContactPerson(hotelInfo.getContactName());
         hotelResVo.setPhotos(imgResVos);
         if(hotelInfo != null){
             return success(hotelResVo);
         }else{
             return Result.error(500, "获取酒店信息失败");
         }

    }

    @Override
    public Result updateHotelInfo(HotelResVo hotelResVo) {
        // 更新数据库
         hotelMapper.updateHotelInfo(hotelResVo);
        // 更新酒店的照片
        // 先删除原来的照片
         hotelMapper.deleteHotelPhoto(hotelResVo.getId());
         // 插入新的照片
         if(hotelResVo.getPhotos() != null){
             for (ImgResVo imgResVo : hotelResVo.getPhotos()) {
                 Img img = new Img();
                 img.setToId(hotelResVo.getId());
                 img.setUrl(imgResVo.getUrl());
                 img.setType(1);
                 img.setName( imgResVo.getName());
                 hotelMapper.insertHotelImg(img);
             }
         }else{
             System.out.println("没有照片");
         }
         return success();
    }

    @Override
    public Result getHotelReviews(String hotelId) {
        // 获取酒店的评论
        List<Comment> comments = hotelMapper.getHotelReviews(hotelId);
        List<CommentRes>  commentReses = new ArrayList<>();
        comments.forEach(  comment -> {
            CommentRes  commentRes = new CommentRes();
            commentRes.setContent(comment.getMessage());
            commentRes.setRating(String.valueOf(comment.getRating()));
            commentRes.setBusinessReply(comment.getReply());
            commentRes.setDate(comment.getTime().format("yyyy-MM-dd"));
            // 查看订单
            Orders orders = hotelMapper.getOrderByOrderUid(comment.getOrderUid());
            commentRes.setUserName(orders.getCustomerName());
            String  roomType = hotelMapper.getRoomTypeByRoomId(orders.getRoomId());
            commentRes.setRoomType(roomType);
            // 用户头像
            String userId = orders.getUserId();
            String avatar = userMapper.selectAvatarById(userId);
            commentRes.setUserAvatar(avatar);
            commentReses.add(commentRes);
        });
        return success(commentReses);
    }

    @Override
    public Result hotelGetComments(String orderUid) {
        Comment comment = hotelMapper.getCommentByOrderUid(orderUid);
        return success(comment);
    }

    @Override
    public Result submitCommentReply(String orderUid, String content) {
        int res = hotelMapper.submitCommentReply(orderUid, content);
        if(res > 0){
            return success();
        }
        return Result.error(500, "回复失败");
    }


}
